﻿using System.Collections.Generic;
using System.Linq;
using QLKDS.Helper;
using QLKDS.DTO;


namespace QLKDS.DAL
{
    public class UserManager
    {

        
        public void CreateUser(User item)
        {
            using (var db = new QLKDSDataContext())
            {
                db.Users.InsertOnSubmit(item);
                db.SubmitChanges();
            }
        }

        public void UpdateUser(User item)
        {
            using (var db = new QLKDSDataContext())
            {
                var user = db.Users.Single(d => d.Id.Equals(item.Id));
                user.Username = item.Username;
                user.Password = item.Password;
                db.SubmitChanges();
            }
   
        }

        public void DeleteUser(long id)
        {

            using (var db = new QLKDSDataContext())
            {
                var user = db.Users.Single(d => d.Id.Equals(id));
                db.Users.DeleteOnSubmit(user);
                db.SubmitChanges();
            }
        }


        public User GetUserById(long id)
        {
            var user = new User();
            using (var db = new QLKDSDataContext())
                {
                    user = db.Users.Single(d => d.Id.Equals(id));
                }

            return user;
        }

        public User GetUserByUsername(string username)
        {
            var userdata = new User();

            using (var db = new QLKDSDataContext())
            {
                userdata = db.Users.Single(d => d.Username.Equals(username));
            }
            return userdata;
        }





        public bool HasUsernameExisted(string username)
        {
            bool UsernameExited = false;

                using (var db = new QLKDSDataContext())
                {
                    var list = (from a in db.Users
                                where a.Username.Equals(username)
                                select a.Id).ToList();

                    UsernameExited = list.Count > 0;
                }

            return UsernameExited;
        }


     
    

        public bool HasUsernameDuplicated(long id, string username)
        {
            bool UsernameDulicapted = false;

                using (var db = new QLKDSDataContext())
                {
                    var list = (from a in db.Users
                                where a.Username.Equals(username)
                                where a.Id.Equals(id)
                                select a.Id).ToList();

                    UsernameDulicapted = list.Count > 0;
                }

            return UsernameDulicapted;

        }

     

      
        public IList<UserInfo> ListUsers()
        {
         
           using (var db = new QLKDSDataContext())
                {

                    var listdata = from d in db.Users
                               where d.Status.Equals(1)
                               select new UserInfo { Username = d.Username};
                    return listdata.ToList();     
           }

           
        }

        /// <summary>
        /// Check login
        /// </summary>
        /// <param name="username"></param>
        /// <param name="password"></param>
        /// <param name="role"></param>
        /// <returns></returns>
        public bool CheckLogin(string username, string password)
        {
            bool loginSuccessfully = false;
    

                using (var db = new QLKDSDataContext())
                {
                    var user = db.Users.FirstOrDefault(u => u.Username.Equals(username) && u.Password.Equals(EncryptionPassword.GetMD5(password)));

                    loginSuccessfully = user != null;
                   
                  
                }

            return loginSuccessfully;
        }

    }
}
